<?php
namespace Ucmaim\Controller\Home;
use Think\Controller;

class CommonController extends Controller {
    // 验证码手机号码格式正则
    protected $mobile_preg = '/^1[34578][0-9]{9}$/';
    protected $member_info;
    protected $current_url;

    public function _initialize(){
        $this->member_info = $this->getMemberInfo();
        if( ! is_null($this->member_info) ){
            $this->assign('member_info', $this->member_info);
        }

        $this->current_url = get_current_url();            //获取当前网址
        $this->assign('current_url', $this->current_url);  //编译到模版
    }

    // 获取已登录会员信息
    protected function getMemberInfo(){
        $session = session('member_info');
        return !empty($session) ? $session : null;
    }

    // 检测是否已登录
    protected  function requireLogin(){
        $session = session('member_info');
        if(empty($session)){
            $this->redirect('Home/Member/Login');
        }
    }

    //验证验证码
    protected function chkVerifyCode($code, $reset = true){
        $config['reset'] = true === $reset ? true : false;
        $Verify = new \Think\Verify($config);
        $result =  $Verify->check($code);
        return $result;
    }

    protected function isExistMobile($mobile){
        $M = M('Member');
        $map['member_mobile'] = $mobile;
        $rowCount = $M->where($map)->Count();
        return $rowCount > 0 ? 1 : 0;
    }


    //发送手机验证码
    protected function sendSMSCode($mobile, $content){
        $M = M('verifycode');
        $returns = [];

        //检验发送时间间隔
        $map['code_bind'] = $mobile;
        $data = $M->field('code_time')->order('code_id desc')->where($map)->find();
        $last = (int)$data['code_time'];

        $C = C('SMS');
        $sms_code  = $this->_getCode();

        $content = str_replace('{$code}', $sms_code, $content);
        if( ! C('APP_DEBUG') ){
            if( time() - $last <= $C['GAP'] )  {
                $returns['code'] = -1;
                $returns['msg']  = '发送过于频繁，请稍候再试';
                return $returns;    //发送过于频繁
            }
            $post_data = array(
                'account'  => $C['ACCOUNT'],
                'password' => $C['PASSWORD'],
                'mobile'   => $mobile,
                'content'  => $content,
            );
            $curl_result = xml_to_array(curl_post($C['URL'], $post_data))['SubmitResult']['code'];  // 请求服务器
        }else{
            $curl_result = 2;
            $returns['debug'] = $sms_code;
        }

        if( $curl_result == 2 ){
            //发送成功，写入数据库
            $M->where($map)->delete();
            $data['code_bind']   = $mobile;
            $data['code_number'] = $sms_code;
            $data['code_time']   = time();

            if( $M->create($data) ){
                $M->add();
                $returns['code'] = 0;
                $returns['msg']  = '发送成功';
                return $returns;    //发送成功
            }else{
                $returns['code'] = -1;
                $returns['msg']  = '接口发送成功，但保存验证码到数据库失败';
                return $returns;    //失败
            }
        }else{
            $returns['code'] = -2;
            $returns['msg']  = '接口发送返回失败';
            return $returns;    //失败
        }
    }

    //验证手机验证码
    protected function chkSMSCode($mobile, $code, $reset = true){
        $M = M('verifycode');
        $map['code_bind'] = $mobile;
        $data = $M->field('code_number, code_time')->where($map)->order('code_time desc')->find();
        if( empty($data) ) return false;
        if($code !== $data['code_number']){
            return false;
        }
        $C = C('SMS');
        $expired = $C['EXPIRED'] * 60;
        $send_time = $data['code_time'];
        if( time() - $send_time > $expired ) return false;
        if( true === $reset ){
            $M->where($map)->delete();
        }
        return true;
    }

    //分页调用
    protected function tpl_page($total, $pageSize = 10){

    }

    //生成手机验证码
    private function _getCode(){
        $len  = 6;   //长度
        $code = '';
        for($i = 0; $i < $len; $i ++){
            $code .=  rand(0, 9);
        }
        return $code;
    }
}
